package com.ruicar.afs.cloud.bizcommon.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinDiscountDetails;
import com.ruicar.afs.cloud.bizcommon.business.mapper.ApplyDiscountDetailsMapper;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyDiscountDetailsService;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import com.ruicar.afs.cloud.parameter.commom.enums.DiscountType;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 贴息明细
 * @author: sijun.yu
 * @date: 2020/5/16 11:47
 */
@Service
@AllArgsConstructor
public class ApplyDiscountDetailsServiceImpl extends ServiceImpl<ApplyDiscountDetailsMapper, FinDiscountDetails> implements ApplyDiscountDetailsService {

    private UidGenerator uidGenerator;

    /**
     * 保存贴息明细列表
     *
     * @param applyNo
     * @param discountList
     */
    public void saveApplyDiscountDetailsList(String applyNo, Long costId, String costType, String discountType, BigDecimal totalDiscountAmt, List<FinDiscountDetails> discountList) {

        if (discountList != null && discountList.size() > 0) {
            BigDecimal discountAmt = totalDiscountAmt;
            BigDecimal hundred = new BigDecimal("100");
            for (int i = 0;i<discountList.size();i++) {
                FinDiscountDetails discountCondition = discountList.get(i);
                FinDiscountDetails discountDetails = new FinDiscountDetails();
                discountDetails.setId(uidGenerator.getUID());
                discountDetails.setApplyNo(applyNo);
                discountDetails.setCostId(costId);
                discountDetails.setCostType(costType);
                discountDetails.setDiscountId(discountCondition.getDiscountId());
                discountDetails.setDiscountParty(discountCondition.getDiscountParty());
                discountDetails.setDiscountScale(discountCondition.getDiscountScale());
                discountDetails.setMaxDiscountAmt(discountCondition.getMaxDiscountAmt());
                if(DiscountType.NORMAL.getIndex().equals(discountType)){
                    if(i==discountList.size()-1){
                        discountDetails.setDiscountAmt(discountAmt);
                    }else{
                        // 按比例计算
                        if(ObjectUtil.isNotNull(discountCondition.getDiscountScale())){
                            BigDecimal currentAmt = discountAmt.multiply(discountCondition.getDiscountScale()).divide(hundred,2,BigDecimal.ROUND_HALF_DOWN);
                            if(discountCondition.getMaxDiscountAmt()!=null && currentAmt.compareTo(discountCondition.getMaxDiscountAmt())>=0){
                                currentAmt = discountCondition.getMaxDiscountAmt();
                                hundred = hundred.subtract(discountCondition.getDiscountScale());
                            }
                            discountDetails.setDiscountAmt(currentAmt);
                        }else{
                            if(discountAmt.compareTo(discountCondition.getMaxDiscountAmt())>=0){
                                discountDetails.setDiscountAmt(discountCondition.getMaxDiscountAmt());
                            }else{
                                discountDetails.setDiscountAmt(discountAmt);
                            }
                        }
                        discountAmt = discountAmt.subtract(discountDetails.getDiscountAmt());
                    }
                }else {
                    discountDetails.setDiscountAmt(discountCondition.getDiscountAmt());
                }

                discountDetails.setTotalToTotal(discountCondition.getTotalToTotal());
                baseMapper.insert(discountDetails);
            }
        }
    }

    /**
     * 查询贴息列表
     *
     * @param applyNo
     * @param costType
     * @return
     */
    public List<FinDiscountDetails> getDiscountDetailsList(String applyNo, String costType) {

        List<FinDiscountDetails> discountList = this.baseMapper.selectList(Wrappers.<FinDiscountDetails>query().lambda()
                .eq(FinDiscountDetails::getApplyNo, applyNo)
                .eq(FinDiscountDetails::getCostType, costType));

        return discountList;
    }

    /**
     * 通过申请编号查询贴息列表
     * @param applyNo
     * @return
     */
    public List<FinDiscountDetails> getDiscountDetailsList(String applyNo){

        List<FinDiscountDetails> discountList = this.baseMapper.selectList(Wrappers.<FinDiscountDetails>query().lambda()
                .eq(FinDiscountDetails::getApplyNo, applyNo));
        return discountList;
    }

    /**
     * 通过申请编号、费用ID、费用类型查询贴息列表
     *
     * @param applyNo
     * @param costId
     * @param costType
     * @return
     */
    public List<FinDiscountDetails> getDiscountDetailsList(String applyNo, Long costId, String costType) {

        List<FinDiscountDetails> discountList = this.baseMapper.selectList(Wrappers.<FinDiscountDetails>query().lambda()
                .eq(FinDiscountDetails::getApplyNo, applyNo)
                .eq(FinDiscountDetails::getCostType, costType));
        return discountList;
    }

    /**
     * 通过申请编号和费用ID查询贴息列表
     *
     * @param applyNo
     * @param costId
     * @return
     */
    public List<FinDiscountDetails> getDiscountDetailsList(String applyNo, Long costId) {

        List<FinDiscountDetails> discountList = this.baseMapper.selectList(Wrappers.<FinDiscountDetails>query().lambda()
                .eq(FinDiscountDetails::getApplyNo, applyNo).eq(FinDiscountDetails::getCostId, costId));
        return discountList;
    }
}
